package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.R
import yja.gzw.app.adapter.FabuAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityGongnengyichangBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.window.Chicun
import java.io.File

class GongnengyichangActivity :BaseActivity<Mainviewmodel, ActivityGongnengyichangBinding>(R.layout.activity_gongnengyichang), FabuAdapter.OnItemClickListener{

    var chicun= Chicun()
    private lateinit var fabiada: FabuAdapter
    public var list= mutableListOf<ZaoShuju>(
        ZaoShuju(2,""),

        )
    var file: File?=null
    override fun initView() {
        chicun.chicun(binding.xuan1,0,40)
        chicun.chicun(binding.xuan2,0,40)
        chicun.chicun(binding.xuan3,0,40)
        chicun.chicun(binding.xuan4,0,40)
        chicun.chicun(binding.xuan5,0,40)
        chicun.chicun(binding.xuan6,0,40)
        chicun.chicun(binding.xuan7,0,40)
        chicun.chicun(binding.xuan8,0,40)
        chicun.chicun(binding.xuan9,0,40)

        chicun.chicunwai(binding.xuan1,0,0,0,0)
        chicun.chicunwai(binding.xuan2,15,0,0,0)
        chicun.chicunwai(binding.xuan3,15,0,0,0)
        chicun.chicunwai(binding.xuan4,-375,50,0,0)
        chicun.chicunwai(binding.xuan5,15,50,0,0)
        chicun.chicunwai(binding.xuan6,15,50,0,0)
        chicun.chicunwai(binding.xuan7,-375,100,0,0)
        chicun.chicunwai(binding.xuan8,15,100,0,0)
        chicun.chicunwai(binding.xuan9,15,100,0,0)




        binding.review.layoutManager=
            GridLayoutManager(this, 3)
        fabiada= FabuAdapter(this,list,this)
        binding.review.adapter= fabiada




        binding.tijiao.setOnClickListener {
            var ids ="1"
            if(binding.xuan1.isChecked){
                ids ="1"
            }else if(binding.xuan2.isChecked){
                ids ="2"
            }else if(binding.xuan3.isChecked){
                ids ="3"
            }else if(binding.xuan4.isChecked){
                ids ="4"
            }else if(binding.xuan5.isChecked){
                ids ="5"
            }else if(binding.xuan6.isChecked){
                ids ="6"
            }else if(binding.xuan7.isChecked){
                ids ="7"
            }else if(binding.xuan8.isChecked){
                ids ="8"
            }else{
                ids ="9"
            }
            val neirong= binding.shurukuang.text.toString()
            var imagess=""
            for ((index,zaoShuju) in list.withIndex()) {
                    if(index==list.size-1){
                        imagess=imagess+zaoShuju.tupian
                    }else{
                        imagess=imagess+zaoShuju.tupian+","
                    }
            }
            val xingming= binding.xiangming.text.toString()
            val dianhua= binding.dianhua.text.toString()
            viewmodel.onfankuiwang(UserData.token, mapOf("content" to neirong,"type_id" to ids,"images"
                    to imagess,"contact" to dianhua,"username" to xingming,"type" to "1"))
        }




    }

    override fun dataObservese() {
        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){



                list.add(0,ZaoShuju(1,it.data.url))
                Log.e("上传图片的半路径",it.data.url)
                binding.review.layoutManager=
                    GridLayoutManager(this, 3)
                fabiada= FabuAdapter(this,list,this)
                binding.review.adapter= fabiada

                Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()




            }


        })

        viewmodel.onfankui.observe(this, Observer {
            if(it.code==1){
                finish()
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })

    }

    override fun onItemClick(position: Int, uri: ZaoShuju) {
        if(uri.leixing==2){
            openGallery()

        }else{
            Toast.makeText(this, "图片预览中"+"    "+list.size, Toast.LENGTH_SHORT).show()


        }

    }
    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }

}